package com.ywy.search.mq;

import com.ywy.config.RabbitMqConfig;
import com.ywy.search.service.ESService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * RabbitMQ消费端
 * 监听异步消息队列，更新搜索内容
 *
 * @author ywy
 * @date 2021/12/30 16:13
 */
@Slf4j
@Component
@RabbitListener(queues = RabbitMqConfig.ES_QUEUE)
public class MqMessageHandler {
    @Autowired
    private ESService esService;

    @RabbitHandler
    public void handler(MqPostMessage message) {
        log.info("mq收到一条消息：{}", message.toString());

        switch (message.getType()) {
            case MqPostMessage.CREATE_OR_UPDATE:
                esService.createOrUpdateIndex(message);
                break;
            case MqPostMessage.REMOVE:
                esService.removeIndex(message);
                break;
            default:
                log.warn("没有找到对应的消息类型：{}", message.toString());
                break;
        }
    }
}
